<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://cdn.jsdelivr.net/npm/vue@2"></script>
    <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
    <script src="https://unpkg.com/element-ui/lib/index.js"></script>
</head>
<body>
<div id="app" >
    <h1>restful风格接口测试-增删改查</h1>
    <div>
        <div>
            <button @click="add()">添加</button>
            <div>{{addMsg}}</div>
        </div>
        <div>
            <button @click="update()">修改</button>
            <div>{{updateMsg}}</div>
        </div>
        <div>
            <button @click="del()">删除</button>
            <div>{{delMsg}}</div>
        </div>
        <div>
            <button @click="select()">查询</button>
            <div>{{selectMsg}}</div>
        </div>
    </div>
    <h1>表单验证传递json数据</h1>
    <el-form ref="form" :model="usermodel" label-width="80px">
        <el-form-item label="id">
            <el-input placeholder="请输入id" v-model="usermodel.id"></el-input>
        </el-form-item>
        <el-form-item label="姓名">
            <el-input placeholder="请输入姓名" v-model="usermodel.name" ></el-input>
        </el-form-item>
        <el-form-item label="密码">
            <el-input placeholder="请输入密码" v-model="usermodel.pwd" show-password></el-input>
        </el-form-item>
        <el-form-item label="年龄">
            <el-input placeholder="请输入年龄" v-model="usermodel.age" ></el-input>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="login">登录</el-button>
            <el-button>取消</el-button>
        </el-form-item>
    </el-form>
    <div>响应user信息:{{user}}</div>
</div>
<script>
    new Vue({
        el: '#app',
        data: {
            usermodel:{
                id:'',
                name:'',
                pwd:'',
                age:''
            },
            addMsg: "addMsg",
            updateMsg:"updateMsg",
            delMsg:"delMsg",
            selectMsg:"selectMsg",
            user:{}
        },
        methods: {
            login(){
                axios.post('http://localhost:8080/login', this.usermodel)
                    .then(response => {
                        console.log("Response Data:", response.data);
                        this.user = response.data;
                        // 处理响应数据
                    })
                    .catch(error => {
                        console.error("Error:", error);
                    });
            },
            add() {
                axios.post('http://localhost:8080/rest/add')
                    .then(response => {
                        this.addMsg = response.data;
                        console.log(this.addMsg);
                    })
                    .catch(error => {
                        this.error = '发生错误: ' + error.message;
                    })
                    .finally(() => {
                        this.loading = false;
                    });
            },
            del() {
                axios.delete('http://localhost:8080/rest/del')
                    .then(response => {
                        this.delMsg = response.data;
                        console.log(this.delMsg);
                    })
                    .catch(error => {
                        this.error = '发生错误: ' + error.message;
                    })
                    .finally(() => {
                        this.loading = false;
                    });
            },
            update() {
                axios.put('http://localhost:8080/rest/update')
                    .then(response => {
                        this.updateMsg = response.data;
                        console.log(this.updateMsg);
                    })
                    .catch(error => {
                        this.error = '发生错误: ' + error.message;
                    })
                    .finally(() => {
                        this.loading = false;
                    });
            },
            select() {
                axios.get('http://localhost:8080/rest/find')
                    .then(response => {
                        this.selectMsg = response.data;
                        console.log(this.selectMsg);
                    })
                    .catch(error => {
                        this.error = '发生错误: ' + error.message;
                    })
                    .finally(() => {
                        this.loading = false;
                    });
            }
        }
    });
</script>
</body>
</html>